package com.bangdream.michelia.view.fragment.main.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.MessageContract;
import com.bangdream.michelia.entity.MessageBean;
import com.bangdream.michelia.entity.UserBean;
import com.bangdream.michelia.presenter.MessagePersenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.my.MessageDetails;
import com.bangdream.michelia.view.adapter.MessageAdapter;
import com.bangdream.michelia.view.adapter.MyMessageListAdapter;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;

import java.util.List;

/**
 * @date on 2018/8/11 13:32
 * @email 909732184@qq.com
 */
public class MyMessageFragment extends LazyLoadFragment<MessageContract.IMessageView, MessagePersenter<MessageContract.IMessageView>>
        implements MyMessageListAdapter.OnItemClickListener, MessageContract.IMessageView {
    private View mView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private XRefreshView refreshView;
    private MyMessageListAdapter adapter;
    MessageAdapter messageAdapter;

    private int type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recycler_view_layout, container, false);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    private void initView() {
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
        refreshView = mView.findViewById(R.id.refreshView);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(false);
        recyclerView = mView.findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);

        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

        messageAdapter = new MessageAdapter(R.layout.my_message_list_item);
        recyclerView.setAdapter(messageAdapter);
    }

    @Override
    public MessagePersenter createPresenter() {
        return new MessagePersenter();
    }

    @Override
    public void loadData() {
//        messageAdapter.setEmptyView(R.layout.loading_view, (ViewGroup)recyclerView.getParent());
        mPresenter.getUserMessageList(1, PublicResources.PageSize, type, pd);

//        if (type == 1) {
//        } else {
//            mPresenter.getSystemMessageList(1, PublicResources.PageSize, userBean.getId(), pd);
//        }
    }

    @Override
    public void onMessageItemClick(int position) {
        Intent intent = new Intent(getActivity(), MessageDetails.class);
        //intent.putExtra("data",adapter.getData().get(position));
        startActivity(intent);
    }

    @Override
    public void setUserMessageList(boolean isSuccess, int page, List<MessageBean> data, String stDescribe) {
        if (isSuccess) {
            if (null == data || data.size() == 0) {
                messageAdapter.setEmptyView(noDataView);
            } else {
                messageAdapter.setNewData(data);
            }
        } else {
            messageAdapter.setEmptyView(errorView);
        }
    }

    @Override
    public void setSystemMessageList(boolean isSuccess, int page, List<MessageBean> data, String stDescribe) {

    }

    @Override
    public void actionRead(boolean isSuccess, String stDescribe) {

    }
}
